<?php

/**
 * @Author: wuchenge
 * @Date: 2020-11-30 15:08:52
 * @email: ligui0506@sina.com
 * @descript: 网络请求
 */

namespace App\Libraries;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

class Http
{
    private $client;

    public function __construct($timeout = 2.0)
    {
        $client = new Client([
            'timeout' => $timeout,
        ]);
        $this->client = $client;
    }

    /**
     * GET 请求
     * @param  [type] $url          [description]
     * @param  array  $parameters   [description]
     * @param  array  $headers      [description]
     * @return [type]               [description]
     */
    public function get($url, $parameters = [], $headers = [])
    {
        $param = [];
        // 请求参数
        if (!empty($parameters)) {
            $param['query'] = $parameters;
        }
        if (!empty($headers)) {
            $param['headers'] = $parameters;
        }
        // 设置头信息
        try {
            $response = $this->client->request('GET', $url, $param);
            if ($response->getStatusCode() != 200) {
                return false;
            }
            $body = $response->getBody();
            $data = $body->getContents();
            return [
                'data'   => $data,
                'status' => true,
            ];
        } catch (RequestException $e) {
            $msg = '';
            if ($e->hasResponse()) {
                $msg = $e->getResponse();
            }
            return [
                'status' => false,
                'msg'    => $msg,
            ];
        }
    }

    public function post(
        $url,
        $parameters = [],
        $type = 'form_params',
        $headers = []
    ) {
        $param = [];
        // 请求参数
        if (!empty($parameters)) {
            $param[$type] = $parameters;
        }
        if (!empty($headers)) {
            $param['headers'] = $parameters;
        }
        // 设置头信息
        try {
            $response = $this->client->request('POST', $url, $param);
            if ($response->getStatusCode() != 200) {
                return false;
            }
            $body = $response->getBody();
            $data = $body->getContents();
            return [
                'data'   => $data,
                'status' => true,
            ];
        } catch (RequestException $e) {
            // echo $e->getRequest();

            $msg = '';
            if ($e->hasResponse()) {
                $msg = $e->getResponse();
            }
            return [
                'status' => false,
                'msg'    => $msg,
            ];
        }
    }

    public function downPicture($avatar, $name = '')
    {
        if (!empty($avatar)) {
            if (strpos($avatar, 'http://') === 0 || strpos($avatar, 'https://') === 0) {
                //忽略SSL错误
                $client = new Client(['verify' => false]);

                if (empty($name)) {
                    $name = md5($avatar);
                }
                $path     = public_path('uploads/store/' . $name . '.jpg');
                $response = $client->get($avatar, ['save_to' => $path]); //保存远程url到文件
                if ($response->getStatusCode() == 200) {
                    return $path;
                }
            }
        }

        return false;
    }
}
